package com.example.miniblog.controller;


import com.example.miniblog.bean.Article;
import com.example.miniblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("")
public class MiniBlogController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("")
    public ModelAndView index(@RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "size",defaultValue = "20") Integer size,
                              Map<String,Object> map){


        PageRequest request = PageRequest.of(page - 1, size);
        Page<Article> articlePage = articleService.findAll(request);



        map.put("articlePage", articlePage.getContent());
        map.put("totalPages",articlePage.getTotalPages());
        map.put("currentPage",page);
        map.put("size",20);

        return new ModelAndView("home/index",map);
    }

    @GetMapping("p")
    public ModelAndView detail(@RequestParam("id") String id,Map<String,Object> map){

       Article article = articleService.findOne(id);


        map.put("article",article);

        return new ModelAndView("home/detail",map);
    }


}
